using UnityEngine;

public class Movement : CoreComponent, ILogicupdateable
{
    public bool CanSetVelocity = true;
    private Vector2 _velocity;


    private Vector2 workspace;
    public Rigidbody2D _rb { get; private set; }
    public Vector2 CurrentVelocity { get; private set; }
    public int FacingDirection { get; private set; }

    #region Unity Callback Funcitons

    protected override void Awake()
    {
        base.Awake();

        core.AddCoreComponent(this);
        _rb = GetComponentInParent<Rigidbody2D>();

        FacingDirection = 1;
    }

    void ILogicupdateable.LogicUpdate()
    {
        CurrentVelocity = _rb.velocity;
    }

    #endregion

    #region Set Functions

    public void SetRigidbodyBodyTypeStatic()
    {
        _rb.bodyType = RigidbodyType2D.Static;
    }

    public void SetRigidbodyBodyTypeDynamic()
    {
        _rb.bodyType = RigidbodyType2D.Dynamic;
    }

    public void SetVelocity(float velocity, Vector2 direction)
    {
        _velocity = velocity * direction;
        _rb.velocity = _velocity;
        CurrentVelocity = _velocity;
    }

    public void SetVelocity(float velocity, Vector2 angle, int direction)
    {
        angle.Normalize();
        _velocity.Set(angle.x * velocity * direction, angle.y * velocity);
        _rb.velocity = _velocity;
        CurrentVelocity = _velocity;
    }

    public void SetVelocityX(float velocity)
    {
        _velocity.Set(velocity, CurrentVelocity.y);
        _rb.velocity = _velocity;
        CurrentVelocity = _velocity;
    }

    public void SetVelocityY(float velocity)
    {
        _velocity.Set(CurrentVelocity.x, velocity);
        _rb.velocity = _velocity;
        CurrentVelocity = _velocity;
    }

    public virtual void SetVelocityZero()
    {
        _rb.velocity = Vector2.zero;
    }

    public void CheckIfShouldFilp(int Xinput)
    {
        if (Xinput != 0 && Xinput != FacingDirection) Filp();
    }

    public void FilpImmediately()
    {
        Filp();
    }

    private void Filp()
    {
        FacingDirection *= -1;
        _rb.transform.Rotate(0f, 180f, 0f);
    }

    #endregion
}